/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MathException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
import org.apache.commons.math.analysis.interpolation.UnivariateRealInterpolator;

/**
 * 
 * @author pable
 */
@Entity
@Table(name = "ACEROS"/*
 * ,uniqueConstraints=@UniqueConstraint(columnNames="NOMBRE")
 */)
@NamedQueries({
    @NamedQuery(name = "Acero.findByNombre", query = "SELECT a FROM Acero a WHERE a.nombre = ?0"),
    @NamedQuery(name = "Acero.findAll", query = "SELECT a FROM Acero a")})
public class Acero implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 510064261035796725L;
    private static final int initialCapacity=14;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ACERO_ID")
    private Long id;
    @Column(name = "nombre", unique = true, nullable = false)
    private String nombre;
    //TODO Crear los arrays con 0s, y interpolar hasta los Ys que no tienen 0s
    @ElementCollection
    private List<Double> xValues = new ArrayList<Double>();
    @ElementCollection
    private List<Double> yValues = new ArrayList<Double>();
    @Transient
    private UnivariateRealFunction function = null;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Acero() {
        xValues=Arrays.asList(150d,200d,250d,300d,400d,500d,600d,650d,700d,750d,800d,850d,900d);
        for(int i=0;i<13;i++){
            yValues.add(Double.valueOf(i));
        }
    }

    private void createInterpolation() throws MathException {
        Double[] x = this.xValues.toArray(new Double[this.xValues.size()]);
        Double[] y = this.yValues.toArray(new Double[this.yValues.size()]);

        UnivariateRealInterpolator interpolator = new SplineInterpolator();
        this.function = interpolator.interpolate(ArrayUtils.toPrimitive(x), ArrayUtils.toPrimitive(y));
    }

    public Double evaluar(int temp) throws FunctionEvaluationException, MathException {
        if (this.function == null) {
            this.createInterpolation();
        }
        return this.function.value(temp);
    }

    public List<Double> getxValues() {
        return this.xValues;
    }

    public void setxValues(List<Double> xValues) {
        this.xValues = xValues;
    }

    public List<Double> getyValues() {
        return this.yValues;
    }

    public void setyValues(List<Double> yValues) {
        this.yValues = yValues;
    }

    public String getNombre() {
        return this.nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    @Override
    public String toString() {
        return nombre;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Acero other = (Acero) obj;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.nombre != null ? this.nombre.hashCode() : 0);
        return hash;
    }

//      @Override
//    public boolean equals(Object object) {
//        // TODO: Warning - this method won't work in the case the id fields are not set
//        if (!(object instanceof NewEntity)) {
//            return false;
//        }
//        NewEntity other = (NewEntity) object;
//        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
//            return false;
//        }
//        return true;
//    }
    
    
    static public String FIND_BY_NOMBRE = "Acero.findByNombre";
    static public String FIND_ALL = "Acero.findAll";
}
